import qs from 'qs';
import { ApiUrl } from './env.js';
import { processParams } from '../unit/common.js';
// import store from '@/store';

const ajaxSync = (opt) => {
  try {
    const token = uni.getStorageSync('user_token');
    // if (token && store.state.userInfo.rank < 2 && window.location.href.split('#')[0].indexOf('pages/join/') === -1) {
    //   uni.reLaunch({
    //     url: '/pages/join/result'
    //   });
    //   return false;
    // }
    if (token || (!token && opt.noAuth)) {
      opt = opt || {};
      opt.url = opt.url || '';
      opt.data = processParams(opt.data) || null;
      const crypto = require('crypto');
      const hash = crypto.createHash('md5');
      opt.data.sign = hash.update(decodeURIComponent(qs.stringify(processParams(opt.data)))).digest('hex');
      opt.method = opt.method || 'POST';
      opt.header = opt.header || {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Token': token
      };
      opt.ApiUrl = opt.apiUrl || ApiUrl;
      opt.success = opt.success || function () {};
      opt.fail = opt.fail || function () {};

      return new Promise((resolve, reject) => {
        uni.request({
          url: opt.ApiUrl + opt.url,
          data: opt.data,
          method: opt.method,
          header: opt.header,
          dataType: 'json',
          success: function (res) {
            if (res.statusCode === 200) {
              if (res.data.status === 0) {
                resolve(res.data.data);
              } else if (res.data.status === 2) {
                uni.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  success: () => {
                    
                  }
                });
              } else if (res.data.status === 4) {
                uni.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  success: () => {
                    setTimeout(() => {
                      uni.reLaunch({
                        url: '/pages/join/result'
                      });
                    }, 2000);
                  }
                });
              } else {
                resolve(res.data.data);
                uni.showToast({
                  title: res.data.msg || '系统繁忙，请稍后重试！',
                  icon: 'none'
                });
              };
            } else {
              uni.showToast({
                title: res.data.error || '系统繁忙，请稍后重试！',
                icon: 'none'
              });
            }
          },
          fail: function () {
            reject();
            uni.showToast({
              title: '系统繁忙，请稍后重试！',
              icon: 'none'
            });
          }
        });
      });
    } else {
      uni.showToast({
        title: '请先登录！',
        icon: 'none',
        success: () => {
          
        }
      });
    }
  } catch (e) {
    // error
    uni.showToast({
      title: '请先登录！'+ JSON.stringify(e),
      icon: 'none',
      success: () => {
        
      }
    });
  }
};

export {
  ajaxSync
}